﻿// ExamArrangeQuery.cpp : 实现文件
//

#include "stdafx.h"
#include "学生管理系统MFC.h"
#include "ExamArrangeQuery.h"
#include "afxdialogex.h"


// CExamArrangeQuery 对话框

IMPLEMENT_DYNAMIC(CExamArrangeQuery, CDialogEx)

CExamArrangeQuery::CExamArrangeQuery(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_EXAMARRANGEQUERY, pParent)
{

}

CExamArrangeQuery::~CExamArrangeQuery()
{
}

void CExamArrangeQuery::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO1, m_combobox1);
	DDX_Control(pDX, IDC_COMBO2, m_combobox2);
	DDX_Control(pDX, IDC_COMBO3, m_combobox3);
	DDX_Control(pDX, IDC_COMBO5, m_combobox4);
	DDX_Control(pDX, IDC_LIST1, m_list);
}


BEGIN_MESSAGE_MAP(CExamArrangeQuery, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON1, &CExamArrangeQuery::OnBnClickedButton1)
END_MESSAGE_MAP()


// CExamArrangeQuery 消息处理程序


BOOL CExamArrangeQuery::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	m_combobox1.SetCurSel(0);
	m_combobox2.SetCurSel(0);
	m_combobox3.SetCurSel(0);
	m_combobox4.SetCurSel(0);
	CRect rect;
	// 获取编程语言列表视图控件的位置和大小   
	m_list.GetClientRect(&rect);
	// 为列表视图控件添加全行选中和栅格风格   
	m_list.SetExtendedStyle(m_list.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
	m_list.InsertColumn(0, _T("课头号"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(1, _T("课程名称"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(2, _T("课程类型"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(3, _T("授课院系"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(4, _T("计划学院院系"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(5, _T("专业"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(6, _T("年级"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(7, _T("班级"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(8, _T("考试人数"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(9, _T("考试时间"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(10, _T("考试地点"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(11, _T("主考教师"), LVCFMT_CENTER, rect.Width() / 3, 0);
	m_list.InsertColumn(12, _T("监考教师"), LVCFMT_CENTER, rect.Width() / 3, 0);
	// TODO:  在此添加额外的初始化

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void CExamArrangeQuery::Query()
{
	m_list.DeleteAllItems();
	CString sql, 学年, 学期, 关键字1, 关键字2, 关键字1C, 关键字2C;
	((CComboBox*)GetDlgItem(IDC_COMBO1))->GetWindowText(学年);
	学年 = 学年.Mid(5, 4);
	((CComboBox*)GetDlgItem(IDC_COMBO2))->GetWindowText(学期);
	((CComboBox*)GetDlgItem(IDC_COMBO3))->GetWindowText(关键字1);
	((CComboBox*)GetDlgItem(IDC_COMBO5))->GetWindowText(关键字2);
	((CComboBox*)GetDlgItem(IDC_EDIT1))->GetWindowText(关键字1C);
	((CComboBox*)GetDlgItem(IDC_EDIT2))->GetWindowText(关键字2C);
	//m_username = _T("2014301500095");
	sql = _T("SELECT DISTINCT 成绩.课头号,课程名,课程类型,授课学院,计划学院院系,课程.专业,课程.教师,课程.年级,课程.班级,考试人数,考试时间,考试地点,主考教师,监考教师 FROM 课程,学生,成绩,教师 WHERE 学生.学号=")+m_username+_T(" AND ([教师].[工号] = [课程].[教师] OR [教师].[工号] = [课程].[主考教师]) AND 成绩.课头号=课程.课头号 AND 成绩.学号=") + m_username+_T(" AND 课程.年级=")+学年+_T(" AND 课程.学期='")+学期+_T("'");
	if (关键字1C != "")
	{
		if(关键字1=="课头号")
			sql += _T(" AND 成绩.课头号='") + 关键字1C + _T("'");
		else if (关键字1 == "考试课程名")
			sql += _T(" AND 课程.课程名 LIKE '%") + 关键字1C + _T("%'");
		else if (关键字1 == "开课学院名")
			sql += _T(" AND 课程.授课学院 LIKE '%") + 关键字1C + _T("%'");
		else if (关键字1 == "学生学院名")
			sql += _T(" AND 学生.院系名称 LIKE '%") + 关键字1C + _T("%'");
		else if (关键字1 == "学生专业名")
			sql += _T(" AND 学生.专业名称 LIKE '%") + 关键字1C + _T("%'");
		else if (关键字1 == "学生年级")
			sql += _T(" AND 学生.年级 LIKE '%") + 关键字1C + _T("%'");
		else if (关键字1 == "任课教师")
			sql += _T(" AND 教师.姓名 LIKE '%") + 关键字1C + _T("%'");
		else if (关键字1 == "主考教师")
			sql += _T(" AND 教师.姓名 LIKE '%") + 关键字1C + _T("%'");
		else if (关键字1 == "考试日期")
			sql += _T(" AND 课程.考试时间 LIKE '%") + 关键字1C + _T("%'");
	}
	if (关键字2C != "")
	{
		if (关键字2 == "课头号")
			sql += _T(" AND 成绩.课头号='") + 关键字2C + _T("'");
		else if (关键字2 == "考试课程名")
			sql += _T(" AND 课程.课程名 LIKE '%") + 关键字2C + _T("%'");
		else if (关键字2 == "开课学院名")
			sql += _T(" AND 课程.授课学院 LIKE '%") + 关键字2C + _T("%'");
		else if (关键字2 == "学生学院名")
			sql += _T(" AND 学生.院系名称 LIKE '%") + 关键字2C + _T("%'");
		else if (关键字2 == "学生专业名")
			sql += _T(" AND 学生.专业名称 LIKE '%") + 关键字2C + _T("%'");
		else if (关键字2 == "学生年级")
			sql += _T(" AND 学生.年级 LIKE '%") + 关键字2C + _T("%'");
		else if (关键字2 == "任课教师")
			sql += _T(" AND 教师.姓名 LIKE '%") + 关键字2C + _T("%'");
		else if (关键字2 == "主考教师")
			sql += _T(" AND 教师.姓名 LIKE '%") + 关键字2C + _T("%'");
		else if (关键字2 == "考试日期")
			sql += _T(" AND 课程.考试时间 LIKE '%") + 关键字2C + _T("%'");
	}
		
	::CoInitialize(NULL);
	g_pDB->Initialize(ConnectionAddress, ConnectionDB, ConnectionUser, ConnectionPassword);
	CSqlStatement *stmt = g_pDB->ExecuteSQL(sql);
	while (stmt != NULL)
	{
		if (stmt->NextRow() == NULL)
		{
			break;
		}
		if (关键字1 == "任课教师")
			if (工号转姓名(_ttoi(stmt->ValueString(_T("教师")))) != 关键字1C)
				continue;
		if (关键字2 == "任课教师")
			if (工号转姓名(_ttoi(stmt->ValueString(_T("教师")))) != 关键字2C)
				continue;
		if (关键字1 == "主考教师")
			if (工号转姓名(_ttoi(stmt->ValueString(_T("主考教师")))) != 关键字1C)
				continue;
		if (关键字2 == "主考教师")
			if (工号转姓名(_ttoi(stmt->ValueString(_T("主考教师")))) != 关键字2C)
				continue;

		int nRow = m_list.InsertItem(0, stmt->ValueString(_T("课头号")));//插入行
		m_list.SetItemText(nRow, 1, stmt->ValueString(_T("课程名")));//设置数据
		m_list.SetItemText(nRow, 2, stmt->ValueString(_T("课程类型")));//设置数据
		m_list.SetItemText(nRow, 3, stmt->ValueString(_T("授课学院")));//设置数据
		m_list.SetItemText(nRow, 4, stmt->ValueString(_T("计划学院院系")));//设置数据
		m_list.SetItemText(nRow, 5, stmt->ValueString(_T("专业")));//设置数据
		m_list.SetItemText(nRow, 6, stmt->ValueString(_T("年级")));//设置数据
		m_list.SetItemText(nRow, 7, stmt->ValueString(_T("班级")));//设置数据
		m_list.SetItemText(nRow, 8, stmt->ValueString(_T("考试人数")));//设置数据
		m_list.SetItemText(nRow, 9, stmt->ValueString(_T("考试时间")));//设置数据
		m_list.SetItemText(nRow, 10, 工号转姓名(_ttoi(stmt->ValueString(_T("主考教师")))));//设置数据
		m_list.SetItemText(nRow, 11, 工号转姓名(_ttoi(stmt->ValueString(_T("监考教师")))));//设置数据
		//m_list.SetItemText(nRow, 10, stmt->ValueString(_T("主考教师")));//设置数据
		//m_list.SetItemText(nRow, 11, stmt->ValueString(_T("监考教师")));//设置数据
	}
	delete stmt;
	::CoUninitialize();
}

void CExamArrangeQuery::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	Query();
}

void CExamArrangeQuery::OnOK()
{

}